function Rinex32Rinex2Single(filena,OutDir,OptSaveRnx,OptName)
%Rinex32Rinex2Single - Single file RINEX3 to RINEX2 conversion (version 2.11)
%
%       Rinex32Rinex2Single(Filena,OutDir,OptSaveRnx,OptName)
%
% This function carries out the RINEX3 to RINEX2 conversion of the input
% file (Hatanaka compressed and gzipped) Filena. For example, if Filena is 
%           'MEDI00ITA_R_20211230000_01D_30S_MO.crx.gz', 
% the output file is 'MEDI1230.21o', placed in the output folder OutDir. 
% A .rnx, non-compressed file can also be used as input file. In this case,
% a possible Filena can be 
%           'MEDI00ITA_R_20211230000_01D_30S_MO.rnx',
% ad the corresponding output is as above.
%
% Notes about the input arguments:
% - if Filena is undefined or empty, the input filename (either .crx.gz 
%   of .rnx) can be managed in interactive way;
% - if OutDir is undefined or empty, the current directory is used;
% - if OptSaveRnx is undefined, empty or false, each RINEX3 file is deleted
%   after the conversion. If OptSaveRnx is true, the RINEX3 files are kept
%   in the folder OutDir;
% - if OptName is undefined, empty or false, the MARKER NAME value in the 
%   output file is the short station name (MEDI in the case of MEDI00ITA).
%   In this case, a file editing is automatically carried out after the 
%   conversion to RINEX2. 
%   If OptName is true, the MARKER NAME value obtained by RINEX3 -> RINEX2 
%   conversion is used.
% 
% This function requires these external packages:
%   - RNXCMP (windows10 64 bit or Linux 64 bit placed on c:\GNSS\RNXCMP\bin
%     or /mnt/c/GNSS/RNXCMP/bin/ directory respectively. If a different 
%     version and/or a different folder should be used, the function must 
%     be edited accordingly).
%     Download link: https://terras.gsi.go.jp/ja/crx2rnx.html
%   - GFZRNX (windows10 64 bit or Linux 64 bit placed on c:\GNSS\RGFZRNX or
%     /mnt/c/GNSS/GFZRNX/ directory respectively. If a different version 
%     and/or a different folder should be used, the function must be edited 
%     accordingly).
%     Download link: https://gnss.gfz-potsdam.de/services/gfzrnx 
%     (registration is necessary)
%
%  See also Rinex32Rinex2, StationNameEdit. 
%
%       Rinex32Rinex2Single(Filena,OutDir,OptSaveRnx,OptName)

% G. Teza, 2022

if nargin < 4 || isempty(OptName)
    OptName = false;
end
if nargin < 3 || isempty(OptSaveRnx)
    OptSaveRnx = false;
end
if nargin < 2 || isempty(OutDir)
    OutDir = pwd;
end
if nargin < 1 || isempty(filena)
    [filena, pathna] = uigetfile(...
        {'*.gz','compressed RINEX 3 files (*.crx.gz)';...
        '*.rnx','RINEX 3 file (*.rnx)'},...
        'INPUT RINEX 3 FILE');
    if ~isequal(filena,0) && ~isequal(pathna,0)
        filena = fullfile(pathna,filena);
    else
        warning('Invalid input file - no conversion');
        return
    end
end

if ispc
    commHata = '"c:\GNSS\RNXCMP\bin\crx2rnx.exe" ';
    commConv = '"c:\GNSS\GFZRNX\gfzrnx_1.16-8154_win10_64.exe" ';
elseif isunix
    commHata = '/mnt/c/GNSS/RNXCMP/bin/crx2rnx ';
    commConv = '/mnt/c/GNSS/GFZRNX/gfzrnx_1.16-8154_lx64 ';  
else
    error('Unsuitable operating system');
end

[~,filenam,ext] = fileparts(filena);
statNameCompl = upper(filenam(1:9));
years = filenam(13:16);
dayks = filenam(17:19);

% Rinex3 filenames management (WF: with output folder path)
if strcmpi(ext,'.gz')
    opt = 1;
    filena3kgz = [filenam ext];
    filenaWF3kgz = fullfile(OutDir,filena3kgz);
    filenaWF3kcrx = filenaWF3kgz(1:end-3);
    filenaWF3krnx = [filenaWF3kcrx(1:end-4) '.rnx'];
    % gunzip:
    gunzip(filena,OutDir);
    delete(filena);
else
    opt = 2;
    filenaWF3krnx = fullfile(OutDir,[filenam '.rnx']);
    if ~strcmp(pwd,OutDir)
        movefile(filena,filenaWF3krnx);
    end
end

% Rinex2 filenames management: 
filena2k = [statNameCompl(1:4)  dayks '0.' years(3:4) 'o'];
filenaWF2k = fullfile(OutDir,filena2k);

fprintf('conversion RINEX3 -> RINEX2 in progress...\n');

if opt == 1
    % Hatanaka decompression:
    commHatak = [commHata filenaWF3kcrx];
    if exist(filenaWF3krnx,'file') % removal of the already decompressed file, if existing
        delete(filenaWF3krnx);
    end
    system(commHatak);  
    delete(filenaWF3kcrx);  % removal of the Hatanaka compressed file
end

% Final conversion:
if exist(filenaWF2k,'file')
    delete(filenaWF2k); % to remove the .yyo file if it already exists
end

commConvk = [commConv ' -finp ' filenaWF3krnx ' -fout ' filenaWF2k ' -vo 2'];
[iexek,~] = system(commConvk);
if iexek == 0
    if ~OptName     % to solve the MARKER NAME problem, if required
        StationNameEdit(filenaWF2k);    
    end
    fprintf('conversion completed\n');
else
    fprintf('unsuccessful conversion\n');
end
% Optional RINEX3 file removal
if ~OptSaveRnx
    delete(filenaWF3krnx);
end